package com.lge.pitchingcoins.util.databases;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class PitchingCoinsDatabaseHandler {
    public static final String NUMBER_OF_HIGHSCORE = "10";
    
    private Context mContext = null;
    private PitchingCoinsDatabaseHelper mDBHelper = null;
    private SQLiteDatabase mDatabase = null;
    
    public PitchingCoinsDatabaseHandler(Context context) {
        mContext = context;
    }
    
    
    // Member Methods
    public void openDatabase() {
        mDBHelper = new PitchingCoinsDatabaseHelper(mContext);
        mDatabase = mDBHelper.getWritableDatabase();
    }
    
    public void closeDatabase() {
        mDBHelper.close();
    }
    
    public long insertNewScore(String nickname, int score) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(PitchingCoinsDatabaseHelper.KEY_NICKNAME, nickname);
        contentValues.put(PitchingCoinsDatabaseHelper.KEY_SCORE, score);
        
        return mDatabase.insert(PitchingCoinsDatabaseHelper.SCORE_TABLE_NAME, null, contentValues);
    }
    
    public Cursor fetchAllScore() {
        return mDatabase.query(PitchingCoinsDatabaseHelper.SCORE_TABLE_NAME, new String[] {PitchingCoinsDatabaseHelper.KEY_NICKNAME, PitchingCoinsDatabaseHelper.KEY_SCORE}, null, null, null, null, PitchingCoinsDatabaseHelper.KEY_SCORE + " desc", NUMBER_OF_HIGHSCORE);
    }
}